package java2d.game.sudoku;

import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class HelpDialog extends JDialog
{
    public HelpDialog(Frame parent)
    {
        super(parent);
        setupUI();
    }

    private void setupUI()
    {
        JPanel root = new JPanel(new MigLayout("gap 0", "[600!, fill, center]"));

        JEditorPane textArea = new JEditorPane();
        textArea.setContentType("text/html");
        textArea.setEditable(false);
        textArea.setEnabled(false);
        textArea.setText("""
                         <!DOCTYPE html>
                         <html lang="zh-CN">
                         <head>
                             <meta charset="UTF-8">
                             <style>
                                 body {
                                     font-size: 14;
                                 }
                                 table {
                                     border-collapse: collapse;
                                     width: 100%;
                                     margin: 0 auto;
                                 }
                                 td {
                                     padding: 2px 4px;
                                     text-align: left;
                                 }
                             </style>
                         </head>
                         <body>
                             <table>
                                 <tr>
                                     <td><strong>方向键、WASD</strong></td>
                                     <td>移动单元格光标</td>
                                 </tr>
                                 <tr>
                                     <td><strong>1~9</strong></td>
                                     <td>设置当前单元格数字</td>
                                 </tr>
                                 <tr>
                                     <td><strong>C</strong></td>
                                     <td>清空当前单元格</td>
                                 </tr>
                                 <tr>
                                     <td><strong>H</strong></td>
                                     <td>显示或隐藏单元格候选数字</td>
                                 </tr>
                                 <tr>
                                     <td></td>
                                     <td></td>
                                 </tr>
                                 <tr>
                                     <td><strong>Ctrl + 1~9</strong></td>
                                     <td>设置单元格候选数字</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Ctrl + C</strong></td>
                                     <td>清空单元格候选数字</td>
                                 </tr>
                                 <tr>
                                     <td></td>
                                     <td></td>
                                 </tr>
                                 <tr>
                                     <td><strong>ESC</strong></td>
                                     <td>退出游戏</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Ctrl + Z</strong></td>
                                     <td>撤销上一步</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Ctrl + G</strong></td>
                                     <td>放弃</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Ctrl + H</strong></td>
                                     <td>查看答案</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Ctrl + S</strong></td>
                                     <td>保存盘面进度</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Ctrl + L</strong></td>
                                     <td>加载历史进度</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Ctrl + R</strong></td>
                                     <td>恢复盘面进度</td>
                                 </tr>
                                 <tr>
                                     <td></td>
                                     <td></td>
                                 </tr>
                                 <tr>
                                     <td><strong>空格</strong></td>
                                     <td>暂停/继续计时</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Ctrl + 回车</strong></td>
                                     <td>重新计时</td>
                                 </tr>
                                 <tr>
                                     <td></td>
                                     <td></td>
                                 </tr>
                                 <tr>
                                     <td><strong>Alt + 1</strong></td>
                                     <td>切换 4x4 盘面，重新开局，难度：困难</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Alt + 2</strong></td>
                                     <td>切换 9x9 盘面，重新开局，难度：困难</td>
                                 </tr>
                                 <tr>
                                     <td><strong>Alt + 3</strong></td>
                                     <td>切换 6x6 盘面，重新开局，难度：困难</td>
                                 </tr>
                                 <tr>
                                     <td></td>
                                     <td></td>
                                 </tr>
                                 <tr>
                                     <td><strong>F1</strong></td>
                                     <td>查看操作说明</td>
                                 </tr>
                                 <tr>
                                     <td><strong>F2</strong></td>
                                     <td>重新开局，难度：困难</td>
                                 </tr>
                                 <tr>
                                     <td><strong>F3</strong></td>
                                     <td>重新开局，难度：很难</td>
                                 </tr>
                                 <tr>
                                     <td><strong>F4</strong></td>
                                     <td>重新开局，难度：极难</td>
                                 </tr>
                                 <tr>
                                     <td><strong>F5</strong></td>
                                     <td>重新开局（空白）</td>
                                 </tr>
                             </table>
                         </body>
                         </html>
                         """);

        root.add(textArea, "wrap");
        root.add(new JSeparator(SwingConstants.HORIZONTAL), "gapy 10, wrap");

        JButton close = new JButton("确定");
        close.addActionListener(_ -> closeDialog());
        JPanel control = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        control.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 20));
        control.add(Box.createVerticalStrut(40));
        control.add(Box.createHorizontalGlue());
        control.add(close);
        root.add(control, "gapy 10, pushy");

        setContentPane(root);

        pack();
        setModal(true);
        setTitle("操作说明");
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        getRootPane().setDefaultButton(close);
        getRootPane().registerKeyboardAction(_ -> closeDialog(),
                                             KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                                             JComponent.WHEN_IN_FOCUSED_WINDOW);
    }

    private void closeDialog()
    {
        setVisible(false);
        dispose();
    }
}
